﻿var dataDepart = "";
var dataYw = "";
$(function () {
    loadYw();
    loadDepartment();
    $("#btnSave").click(function () { save(); });
    
    loadTables(1);
});

//加载列表数据
function loadTables(pageIndex) {
    $("#tableBody").html("");    
    $.ajax({
        type: "GET",
        url: "/Gzh/ZLYJ/GetPagedListToJson?pageindex=" + pageIndex + "&_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data.rows, function (i, item) {
                var tr = "<tr>";
                tr += "<td >" + item.id + "</td>";
                tr += "<td>" + findDepartmentNameByDepartmentId(item.departmentId) + "</td>";
                tr += "<td>" + item.name + "</td>";
                tr += "<td>" + item.tel + "</td>";
                tr += "<td>" + findYwNameByYwId(item.ywId) + "</td>";
                tr += "<td>" + item.createdOn + "</td>";
                tr += "<td>" + (item.isSh == true ? "已审" : "未审") + "</td>";
                tr += "<td>" + (item.shjg == true ? "合格" : "不合格") + "</td>";
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='edit(\"" + item.id + "\")'><i class='fa fa-edit'></i>  </button></td>"
                tr += "</tr>";
                $("#tableBody").append(tr);
            })
            var elment = $("#grid_paging_part"); //分页插件的容器id
            if (data.rowCount > 0) {
                var options = { //分页插件配置项
                    bootstrapMajorVersion: 3,
                    currentPage: pageIndex, //当前页
                    numberOfPages: data.rowCount, //总数
                    totalPages: data.pageCount, //总页数
                    onPageChanged: function (event, oldPage, newPage) { //页面切换事件
                        loadTables(newPage);
                    },
                    itemTexts: function (type, page, current) {
                        switch (type) {
                            case "first": return "首页";
                            case "prev": return "上一页";
                            case "next": return "下一页";
                            case "last": return "末页";
                            case "page": return page;
                        }
                    }
                }
                elment.bootstrapPaginator(options); //分页插件初始化
            }
        }
    })
}

function loadDepartment() {
    $.ajax({
        type: "GET",
        url: "/Sys/Department/GetAllListToJson",
        async: false,       //Ajax请求默认的都是异步的,此处设为同步
        success: function (data) {
            dataDepart = data;
            return true;
        }
    });
}

function findDepartmentNameByDepartmentId(departmentId) {
    var result = "";
    $.each(dataDepart, function (i, item) {
        if (item.id == departmentId) {
            result = item.name;
            //alert(html);
            return result;
        }
    });
    return result;
}

function loadYw() {
    $.ajax({
        type: "GET",
        url: "/Gzh/Yw/GetAllListToJson",
        async: false,       //Ajax请求默认的都是异步的,此处设为同步
        success: function (data) {
            dataYw = data;
        }
    });
}

function findYwNameByYwId(ywId) {
    var result = "";
    $.each(dataYw, function (i, item) {
        if (item.id == ywId) {
            result = item.name;
            //alert(html);
            return result;
        }
    });
    return result;
}

//编辑
function edit(id) {
    $.ajax({
        type: "Get",
        url: "/Gzh/ZLYJ/GetByIdToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $("#CreatedBy").val(data.createdBy);
            $("#CreatedOn").val(data.createdOn);

            $("#Id").val(data.id);           
            $("#YwId").val(data.ywId);  //用于贮值
            $("#YwId1").val(findYwNameByYwId(data.ywId));       //用于显示
            $("#DepartmentId").val(data.departmentId);
            $("#DepartmentId1").val(findDepartmentNameByDepartmentId(data.departmentId));
            $("#Name").val(data.name);
            $("#IdNumber").val(data.idNumber);
            $("#Tel").val(data.tel);
            $("#Sqly").val(data.sqly);
            $("#Pics").val(data.pics);
            $("#img").html("");
            if (data.pics) {
                var picArray = data.pics.split("|");
                for (var item = 1; item < picArray.length; item++) {
                    $("#img").append("<a target='_blank' href='/UploadFiles/" + picArray[item] + "'><img src='/UploadFiles/" + picArray[item] + "' style='width:100px;'></a>");
                }
            }
            $("#PublicTime").val(data.createdOn);
            $("#IsSh").prop("checked", data.isSh);
            $("#Shjg").prop("checked", data.shjg);
            $("#Shsm").val(data.shsm);
            $("#ShMan").val(data.shMan);
            $("#ShTime").val(data.shTime);

            $("#Title").text("查看/编辑");
            $("#editModal").modal("show");
        }
    })
};

//保存
function save() {
    var postData = {
        "CreatedBy": $("#CreatedBy").val(),
        "CreatedOn": $("#CreatedOn").val(),

        "Id": $("#Id").val(),        
        "YwId": $("#YwId").val(),
        "DepartmentId": $("#DepartmentId").val(),
        "Name": $("#Name").val(),
        "IdNumber": $("#IdNumber").val(),
        "Tel": $("#Tel").val(),
        "Sqly": $("#Sqly").val(),
        "Pics": $("#Pics").val(),
        "CreatedOn": $("#PublicTime").val(),
        "IsSh": $("#IsSh").prop('checked'),
        "Shjg": $("#Shjg").prop('checked'),
        "Shsm": $("#Shsm").val(),
        "ShMan": $("#ShMan").val(),
        "ShTime": GetTime()
    };
    $.ajax({
        type: "Post",
        url: "/Gzh/ZLYJ/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadTables(1)
                $("#editModal").modal("hide");
            } else {
                layer.tips(data.message, "#btnSave");
            };
        }
    });
};


function GetTime(date) {
    if (date == null) {
        date = new Date();
    }
    var y = date.getFullYear();
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    //var S = date.getTime() % 1000;
    var html = y + "-";
    if (M < 10) {
        html += "0";
    }
    html += M + "-";

    if (d < 10) {
        html += "0";
    }
    html += d + " ";
    if (h < 10) {
        html += "0";
    }
    html += h + ":";
    if (m < 10) {
        html += "0";
    }
    html += m + ":";
    if (s < 10) {
        html += "0";
    }
    html += s;

    return html;
}